package LeetCode.exercise;

import java.util.ArrayList;
import java.util.List;
import java111.TreeNode;

public class LC257 {
    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(1);
        TreeNode node2 = new TreeNode(2);
        TreeNode node3 = new TreeNode(5);
        TreeNode node4 = new TreeNode(3);
        node1.left = node2;
        node1.right = node4;
        node2.right = node3;
        LC257 lc257 = new LC257();
        List<String> list = lc257.binaryTreePaths(node1);
        System.out.println(list);
    }
    List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if(root == null){
            return res;
        }
        res.add(root.val + "");
        dfs(root);
        return res;
    }
    public void dfs(TreeNode root){
        StringBuilder str1 = new StringBuilder(res.get(res.size() - 1));
        StringBuilder str2 = new StringBuilder(res.get(res.size() - 1));
        if(root.left != null){
            res.remove(str1.toString());
            str1.append("->");
            str1.append(root.left.val);
            res.add(str1.toString());
            dfs(root.left);
        }
        if(root.right != null){
            res.remove(str2.toString());
            str2.append("->");
            str2.append(root.right.val);
            res.add(str2.toString());
            dfs(root.right);
        }
    }
}
